package com.wyl.auth.controller;

import com.wyl.auth.dto.SysRoleDto;
import com.wyl.auth.dto.SysRoleMenuDto;
import com.wyl.auth.req.SysRoleReq;
import com.wyl.auth.service.SysRoleService;
import com.wyl.auth.vo.SysRoleVo;
import com.wyl.common.CommonPage;
import com.wyl.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description 角色
* @Author WuYiLong
* @Date 2025-04-03
*/
@Api(tags = "系统角色")
@RestController
@RequestMapping("sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "保存")
    @PostMapping(value = "save")
    public ResponseData save(@RequestBody SysRoleDto dto) {
        sysRoleService.save(dto);
        return ResponseData.getSuccess();
    }

    @ApiOperation(value = "分页")
    @PostMapping(value = "page")
    public ResponseData<CommonPage<SysRoleVo>> page(@RequestBody SysRoleReq req) {
        return ResponseData.getSuccessInstance(sysRoleService.page(req));
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "deleteByIds")
    public ResponseData deleteByIds(@RequestBody List<Long> ids) {
        sysRoleService.deleteByIds(ids);
        return ResponseData.getSuccess();
    }

    @ApiOperation(value = "分配权限")
    @PostMapping(value = "assignPermission")
    public ResponseData assignPermission(@RequestBody SysRoleMenuDto sysRoleMenuDto) {
        sysRoleService.assignPermission(sysRoleMenuDto);
        return ResponseData.getSuccess();
    }

    @ApiOperation(value = "根据角色获取权限")
    @GetMapping(value = "listPermissionsByRoleId")
    public ResponseData<List<Long>> listPermissionsByRoleId(Long roleId) {
        return ResponseData.getSuccessInstance(sysRoleService.listPermissionsByRoleId(roleId));
    }

    @ApiOperation(value = "角色列表")
    @GetMapping(value = "listRoles")
    public ResponseData<List<SysRoleVo>> listRoles() {
        return ResponseData.getSuccessInstance(sysRoleService.listRoles());
    }
}
